package serialization;
import java.beans.XMLEncoder;
import java.beans.XMLDecoder;
import java.io.*;

/**
 *
 * @author sdecrem
 */

public class Action {
    public static void write(Foo f, String filename) throws Exception{
        XMLEncoder encoder =
           new XMLEncoder(
              new BufferedOutputStream(
                new FileOutputStream(filename)));
        encoder.writeObject(f);
        encoder.close();
    }

    public static void writeGraphe(String filename) throws Exception{
        XMLEncoder encoder =
           new XMLEncoder(
              new BufferedOutputStream(
                new FileOutputStream(filename)));
        encoder.writeObject(GrapheT.getInstance());
        encoder.close();
    }

    public static Foo read(String filename) throws Exception {
        XMLDecoder decoder =
            new XMLDecoder(new BufferedInputStream(
                new FileInputStream(filename)));
        Foo o = (Foo)decoder.readObject();
        decoder.close();
        return o;
    }

    public static GrapheT readGraphe(String filename) throws Exception {
        XMLDecoder decoder =
            new XMLDecoder(new BufferedInputStream(
                new FileInputStream(filename)));
        GrapheT o = (GrapheT)decoder.readObject();
        decoder.close();
        return o;
    }

    public static void readGraphePlus(String filename) throws Exception {
        XMLDecoder decoder =
            new XMLDecoder(new BufferedInputStream(
                new FileInputStream(filename)));
        decoder.readObject();
        decoder.close();
        //return o;
    }

    public static void main (String [] args) throws Exception{
        GrapheT.getInstance().setChemin(0, "chaine1") ;
        GrapheT.getInstance().setChemin(1, "chaine2") ;
        GrapheT.getInstance().setChemin(2, "chaine3") ;
        GrapheT.getInstance().setChemin(3, "chaine4") ;

        GrapheT.getInstance().setVille(0, "chaine5") ;
        GrapheT.getInstance().setVille(1, "chaine6") ;
        GrapheT.getInstance().setVille(2, "chaine7") ;
        GrapheT.getInstance().setVille(3, "chaine8") ;

        GrapheT.getInstance().setLac(0, "chaine9") ;
        GrapheT.getInstance().setRiviere(0, "chaine10") ;

        System.out.println(GrapheT.affiche()) ;
        Action.writeGraphe("/tmp/graphe1.xml");
        System.out.println(GrapheT.affiche()) ;
        /*Foo  f1 = new Foo();
        f1.setFoo("bar");
        f1.addToVector(1) ;
        f1.addToVector(2) ;
        f1.addToVector(4) ;
        f1.addToVector(8) ;
        f1.addToVector(16) ;
        f1.addToVector(32) ;
        Action.write(f1, "/tmp/foo1.xml");

        Foo f2 = Action.read("/tmp/foo1.xml");
        System.out.println(f2);*/
        // the output : Foobar

        /*Graphe simpleTwo = Action.readGraphe("/tmp/graphe1.xml") ;
        System.out.println(simpleTwo) ;
        Action.readGraphePlus("/tmp/graphe1.xml") ;*/

        GrapheT.setInstance(null);
        
        System.out.println(GrapheT.affiche()) ;
        //Graphe.setInstance(Action.readGraphe("/tmp/graphe1.xml")) ;
        //System.out.println(Graphe.affiche()) ;

    }
}

